#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

ll qmi(ll a, ll b)
{
    ll res = 1;
    while(b)
    {
        res = res * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return res;
}

void solve()
{
    int n, m, x;
    cin >> n >> m >> x;
    queue<int> qu;
    qu.push(x - 1);
    while(m--)
    {
        vector<bool>st(n, false);
        queue<int> q;
        int w;
        cin >> w;
        string op;
        cin >> op;
        if(op[0] == '1')
        {
            while(qu.size())
            {
                int t = qu.front();
                qu.pop();
                t = ((t - w) % n + n) % n;
                if(st[t]) continue;
                st[t] = true;
                q.push(t);
            }
        }
        else if(op[0] == '0')
        {
            while(qu.size())
            {
                int t = qu.front();
                qu.pop();
                t = (t + w) % n;
                if(st[t]) continue;
                st[t] = true;
                q.push(t);
            }
        }
        else{
            while(qu.size())
            {
                int t = qu.front();
                qu.pop();
                int t1 = ((t - w) % n + n) % n;
                int t2 = (t + w) % n;
                if(!st[t1])
                {
                    st[t1] = true;
                    q.push(t1);
                }
                if(!st[t2])
                {
                    st[t2] = true;
                    q.push(t2);
                }
            }
        }
        qu = q;
    }

    cout << qu.size() << endl;
    vector<int> ans;
    while(qu.size()){
        ans.push_back(qu.front());
        qu.pop();
    }
    sort(ans.begin(), ans.end());
    for(auto i : ans) cout << i + 1 << ' ';
    cout << endl;
}

int main()
{
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}